using System.Reflection;
using System.Resources;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System;
using System.Windows.Forms;
using Send2Sharepoint.Common;

namespace Send2Sharepoint.Excel
{
    class UploadExcelDocument:WorkItem
    {
        readonly ResourceManager _rm = new ResourceManager("Send2Sharepoint.Resources", Assembly.GetExecutingAssembly());
        public Workbook ExcelWorkbook { get; private set; }

        public UploadExcelDocument(Workbook workbook)
        {
            ExcelWorkbook = workbook;
        }

        internal override void Upload(System.ComponentModel.BackgroundWorker worker, System.ComponentModel.DoWorkEventArgs e)
        {
            if (ExcelWorkbook != null)
            {
                try
                {
                    var fileName = Path.GetTempPath() + ExcelWorkbook.Name;
                    ExcelWorkbook.SaveCopyAs(fileName);
                    var fileData = Globals.GetFileData(fileName);
                    var repository = new Repository.Repository(Globals.UserName, Globals.Password, Globals.SharepointSiteUrl);
                    var destinationUrl = Globals.GetDestinationUrl(ExcelWorkbook.Name);
                    repository.UploadFile2007(destinationUrl, fileData, IndexInformation);
                }
                catch (Exception ex)
                {
                    Logging.WriteException(ex);
                    throw;
                }
            }
            else
            {
                MessageBox.Show(string.Format(_rm.GetString("NotSavedMessage"), "Workbook"), _rm.GetString("ApplicationName"), MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}
